1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.Arrays;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * A container class for the five sample elements we need for testing.
28   *
29   * @author Kevin Bourrillion
30   */
31  @GwtCompatible
32  public class SampleElements<E> implements Iterable<E> {
33    // TODO: rename e3, e4 => missing1, missing2
34    public final E e0;
35    public final E e1;
36    public final E e2;
37    public final E e3;
38    public final E e4;
39  
40    public SampleElements(E e0, E e1, E e2, E e3, E e4) {
41      this.e0 = e0;
42      this.e1 = e1;
43      this.e2 = e2;
44      this.e3 = e3;
45      this.e4 = e4;
46    }
47  
48    @Override
49    public Iterator<E> iterator() {
50      return asList().iterator();
51    }
52    
53    public List<E> asList() {
54      return Arrays.asList(e0, e1, e2, e3, e4);
55    }
56  
57    public static class Strings extends SampleElements<String> {
58      public Strings() {
59        // elements aren't sorted, to better test SortedSet iteration ordering
60        super("b", "a", "c", "d", "e");
61      }
62  
63      // for testing SortedSet and SortedMap methods
64      public static final String BEFORE_FIRST = "\0";
65      public static final String BEFORE_FIRST_2 = "\0\0";
66      public static final String MIN_ELEMENT = "a";
67      public static final String AFTER_LAST = "z";
68      public static final String AFTER_LAST_2 = "zz";
69    }
70  
71    public static class Chars extends SampleElements<Character> {
72      public Chars() {
73        // elements aren't sorted, to better test SortedSet iteration ordering
74        super('b', 'a', 'c', 'd', 'e');
75      }
76    }
77  
78    public static class Enums extends SampleElements<AnEnum> {
79      public Enums() {
80        // elements aren't sorted, to better test SortedSet iteration ordering
81        super(AnEnum.B, AnEnum.A, AnEnum.C, AnEnum.D, AnEnum.E);
82      }
83    }
84  
85    public static class Ints extends SampleElements<Integer> {
86      public Ints() {
87        // elements aren't sorted, to better test SortedSet iteration ordering
88        super(1, 0, 2, 3, 4);
89      }
90    }
91  
92    public static <K, V> SampleElements<Map.Entry<K, V>> mapEntries(
93        SampleElements<K> keys, SampleElements<V> values) {
94      return new SampleElements<Map.Entry<K, V>>(
95          Helpers.mapEntry(keys.e0, values.e0),
96          Helpers.mapEntry(keys.e1, values.e1),
97          Helpers.mapEntry(keys.e2, values.e2),
98          Helpers.mapEntry(keys.e3, values.e3),
99          Helpers.mapEntry(keys.e4, values.e4));
100   }
101 
102   public static class Unhashables extends SampleElements<UnhashableObject> {
103     public Unhashables() {
104       super(new UnhashableObject(1),
105           new UnhashableObject(2),
106           new UnhashableObject(3),
107           new UnhashableObject(4),
108           new UnhashableObject(5));
109     }
110   }
111 
112   public static class Colliders extends SampleElements<Object> {
113     public Colliders() {
114       super(new Collider(1),
115           new Collider(2),
116           new Collider(3),
117           new Collider(4),
118           new Collider(5));
119     }
120   }
121 
122   private static class Collider {
123     final int value;
124 
125     Collider(int value) {
126       this.value = value;
127     }
128 
129     @Override public boolean equals(Object obj) {
130       return obj instanceof Collider && ((Collider) obj).value == value;
131     }
132 
133     @Override public int hashCode() {
134       return 1; // evil!
135     }
136   }
137 }